package com.vitabuybuy.tv.ui.shopcenter

import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.library.common.base.BaseActivity
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityCommodityClassificationBinding
import com.vitabuybuy.tv.ui.home.bidd.SearchBiddingResultActivity
import com.vitabuybuy.tv.ui.home.group.SearchGroupBuyResultActivity
import com.vitabuybuy.tv.ui.shopcenter.adapter.FirstClassificationAdapter
import com.vitabuybuy.tv.ui.shopcenter.adapter.SecondClassificationAdapter
import com.vitabuybuy.tv.ui.shopcenter.viewmodel.ClassificationViewModel
import kotlinx.android.synthetic.main.activity_commodity_classification.*

class CommodityClassificationActivity :
    BaseActivity<ClassificationViewModel, ActivityCommodityClassificationBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_commodity_classification

    private val firstAdapter by lazy { FirstClassificationAdapter() }
    private val classAdapter by lazy { SecondClassificationAdapter() }
    private var from: Int? = FROM_GOODS_CENTER
    private var titleWord: String? = ""
    override fun onGetBundle(bundle: Bundle?) {
        from = bundle?.getInt("from")
        titleWord = bundle?.getString("titleWord")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.commodity_classification))
        mBinding.viewModel = viewModel
        rcvLeft.run {
            adapter = firstAdapter
            layoutManager = LinearLayoutManager(mContext)
        }
        rcvRight.run {
            adapter = classAdapter
            layoutManager = LinearLayoutManager(mContext)
        }
    }

    override fun observeData() {
        viewModel.getClassification()
        viewModel.firstCateList.observe(this, Observer {
            if (it.size > 0) {
                titleWord?.run {
                    for ((index, bean) in it.withIndex()) {
                        if (this == bean.name) {
                            it[index].isCheck = true
                            firstAdapter.setNewData(it)
                            classAdapter.setNewData(it[index].children)
                        }
                    }
                } ?: run {
                    it[0].isCheck = true
                    firstAdapter.setNewData(it)
                    classAdapter.setNewData(it[0].children)
                }
            }
        })
    }

    override fun eventHandling() {
        firstAdapter.setOnItemClickListener { _, item, position ->
            classAdapter.setNewData(item.children)
            viewModel.firstCateList.value?.run {
                for (cate in this) {
                    cate.isCheck = false
                }
                this[position].isCheck = true
                firstAdapter.setNewData(this)
            }
        }
        classAdapter.onClick = {
            when (from) {
                FROM_GOODS_CENTER -> {
                    CateSearchGoodsResultActivity.openActivity(mContext, it, null, null, null)
                }
                FROM_GROUP_BUY -> {
                    SearchGroupBuyResultActivity.openActivity(mContext, it, null)
                }
                FROM_BIDDING -> {
                    SearchBiddingResultActivity.openActivity(mContext, it, null)
                }
            }
        }
    }

    companion object {
        const val FROM_GOODS_CENTER = 1001
        const val FROM_GROUP_BUY = 1002
        const val FROM_BIDDING = 1003
        fun openActivity(mContext: BaseActivity<*, *>, from: Int) {
            mContext.startActivity(
                bundleOf(Pair("from", from)),
                CommodityClassificationActivity::class.java
            )
        }

        fun openActivity(mContext: BaseActivity<*, *>, titleWord: String?, from: Int) {
            mContext.startActivity(
                bundleOf(Pair("titleWord", titleWord), Pair("from", from)),
                CommodityClassificationActivity::class.java
            )
        }
    }
}
